package com.jxc.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.jxc.model.vo.VMember;

public class ExportMember {

	private List<VMember> list;
	private String filename;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private File file;
	
	public ExportMember(List<VMember> list,String filename,HttpServletRequest request,HttpServletResponse response){
		
		this.list=list;
		this.filename=filename;
		this.request=request;
		this.response=response;
		
		//获取服务器路径
		String path=request.getRealPath("/");
		path+="/tmp";
		
		File tmp=new File(path);
		if(!tmp.exists()){
			System.out.println(path);
			tmp.mkdirs();
		}
		
		this.file=new File(path+"/"+filename);
		if(!file.exists()){
			try {
				file.createNewFile();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	public void export() throws Exception{
		
		WritableWorkbook book = Workbook.createWorkbook(this.file);
		//创建一张表
		WritableSheet sheet = book.createSheet("会员信息表", 0);
		
		//表头
		//								会员编号	名称 、	会员类别、	手机	、		生日A、		生日B		消费  	积分		地址 		备注		办卡日期		所在店铺
		String [] head=new String[]{"会员卡号","名称","会员类别","折扣","手机","生日","消费金额","积分","地址","备注","办卡日期","经销商","仓库"};
		//String [] head=new String[]{"serial","name","memberType","mobile","birthdayA","birthdayB","consume","point","address","remark","openDate","storename"};
		
		for(int i=0;i<head.length;i++){
			
			add(sheet, i, 0, head[i]);
		}
		
		//添加数据
		
		for(int i=0;i<list.size();i++){
			VMember vMember=(VMember)list.get(i);
			
			int row=i+1;
			add(sheet, 0, row, vMember.getSerial());	//编号
			add(sheet, 1, row, vMember.getName());	//姓名
			add(sheet, 2, row, vMember.getTname());	//类别
			add(sheet, 3, row, vMember.getDiscountNum());	//类别
			add(sheet, 4, row, vMember.getMobile());	//手机
			add(sheet, 5, row, vMember.getBirthdayA());	//生日A
			//add(sheet, 5, i, vMember.getBirthdayB());	//生日B
			add(sheet, 6, row, vMember.getConsume());		//消费
			add(sheet, 7, row, vMember.getPoint());		//积分
			add(sheet, 8, row, vMember.getAddress());		//地址
			add(sheet, 9, row, vMember.getRemark());		//备注
			add(sheet, 10, row, vMember.getOpenDate());	//办卡日期
			add(sheet, 11, row, vMember.getUname());	//经销商
			add(sheet, 12, row, vMember.getStorename());	//所在店铺
			
		}
	
		
		//输出
		
		
		book.write();
		book.close();
		
		//将文件通过文件流的方式输出到客户端
		//--
		//设置response对象的相关信息
		
		//输出类型
		response.setContentType("application/x-msdownload");
		
		//文件大小
		response.setContentLength((int) file.length());	
		
		//文件名
		response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(), "iso-8859-1"));

		   // 从response对象中得到输出流,准备下载
		   ServletOutputStream myout = response.getOutputStream();
		   FileInputStream stream=new FileInputStream(file);
		   byte data[]=new byte[4096];
		   int size=0;
		   size=stream.read(data);
		  
		   // 开始循环写出数据
		   while (size!=-1) {
		    myout.write(data,0,size);
		    size=stream.read(data);
		   }
		   
		   myout.flush();
		   myout.close();
		   stream.close();
		   
		   //删除文件
		   file.delete();
		
	}
	
	
	private void add(WritableSheet sheet,int r,int l,Object value){
		Label label=new Label(r,l,value!=null?value.toString():"");
		try {
			
			sheet.addCell(label);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}
